package cn.agiledata.bank.linkbank.impl.icbc;

import java.math.BigDecimal;
import java.util.Date;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;

/**
 * 中国工商银行网上银行b2c支付请求
 * 
 * @author maxianbo
 * 
 */
public final class IcbcB2cNetPayRequest extends IcbcNetPayRequest {

    protected void init() {
        setParameter(IcbcB2cNetConstant.INTERFACE_NAME, "ICBC_PERBANK_B2C"); // 接口名称
        setParameter(IcbcB2cNetConstant.INTERFACE_VERSION, "1.0.0.0"); // 接口版本号
        setParameter(IcbcB2cNetConstant.CURRENCY_TYPE,
                IcbcB2cNetConstant.CURRENCY_TYPE_CNY);// 货币类型：人民币
        setParameter(IcbcB2cNetConstant.VERIFY_JOIN,
                IcbcB2cNetConstant.VERIFY_JOIN_NO);// 检验联名：否
        setParameter(IcbcB2cNetConstant.NOTIFY_TYPE,
                IcbcB2cNetConstant.NOTIFY_TYPE_NOTIFY);// 通知类型:通知
        setParameter(IcbcB2cNetConstant.RESULT_TYPE,
                IcbcB2cNetConstant.RESULT_TYPE_ALWAYS);// 结果类型：始终通知
    }

    protected final AbstractBankPayRequest getCopiedInstance() {
        IcbcNetPayRequest request = new IcbcB2cNetPayRequest();
        request.certFilePath = this.certFilePath;
        request.keyFilePath = this.keyFilePath;
        request.cryptoKeyValidator = this.cryptoKeyValidator;
        return request;
    }

    public void setMerchantId(String merchantId) {
        setParameter(IcbcB2cNetConstant.MERCHANT_ID, merchantId);
    }

    public void setMerchantAccount(String merchantAccount) {
        setParameter(IcbcB2cNetConstant.MERCHANT_ACCOUNT, merchantAccount);
    }

    public String getMerchantUrl() {
        return getParameter(IcbcB2cNetConstant.MERCHANT_URL);
    }

    public void setMerchantUrl(String merchantUrl) {
        merchantUrl = handleMerchantUrl(merchantUrl);
        setParameter(IcbcB2cNetConstant.MERCHANT_URL, merchantUrl);
    }

    public void setOrderId(String orderId) {
        setParameter(IcbcB2cNetConstant.ORDER_ID, orderId);
    }

    public void setAmount(BigDecimal amount) {
        BigDecimal hundred = new BigDecimal(100.0d);
        amount = amount.multiply(hundred).setScale(0);
        setParameter(IcbcB2cNetConstant.AMOUNT, amount.toString());
        setParameter(IcbcB2cNetConstant.CARRIAGE_AMOUNT, amount.toString());
    }

    public void setDate(Date date) {
        String dateValue = DateUtil.date2String(date,
                IcbcB2cNetConstant.DATE_FORMAT);
        setParameter(IcbcB2cNetConstant.DATE, dateValue);
    }

    public void setRemark(String remark) {
        setParameter(IcbcB2cNetConstant.REMARK, remark);
    }

    public void setDigitalSign(String digitalSign) {
        setParameter(IcbcB2cNetConstant.REQUEST_DIGITAL_SIGN, digitalSign);
    }

    public void setCertCode(String certCode) {
        setParameter(IcbcB2cNetConstant.CERT_CODE, certCode);
    }

    public String generateDigitalSign(String cryptoKey)
            throws LinkBankException {
        String certCode = IcbcNetPayUtils.getPublicKeyCode(this.certFilePath);
        setCertCode(certCode);
        String[] keys = { IcbcB2cNetConstant.INTERFACE_NAME,
                IcbcB2cNetConstant.INTERFACE_VERSION,
                IcbcB2cNetConstant.MERCHANT_ID,
                IcbcB2cNetConstant.MERCHANT_ACCOUNT,
                IcbcB2cNetConstant.MERCHANT_URL,
                IcbcB2cNetConstant.NOTIFY_TYPE, IcbcB2cNetConstant.ORDER_ID,
                IcbcB2cNetConstant.AMOUNT, IcbcB2cNetConstant.CURRENCY_TYPE,
                IcbcB2cNetConstant.RESULT_TYPE, IcbcB2cNetConstant.DATE,
                IcbcB2cNetConstant.VERIFY_JOIN };
        String digitalSign = IcbcNetPayUtils.getDigitalSign(getParameters(),
                keys, cryptoKey, this.keyFilePath, false);
        setDigitalSign(digitalSign);
        return digitalSign;
    }

}
